//
//  WBNavigationController.swift
//  微博
//
//  Created by 康智桐 on 2018/3/27.
//  Copyright © 2018年 Kk. All rights reserved.
//

import UIKit

class WBNavigationController: UINavigationController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //隐藏默认的navigationBar
        navigationBar.isHidden = true
    }
    
    
//重写push方法
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if childViewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
          
            //判断控制器的类型
            if let vc = viewController as? WBBaseViewController{
                
                var title = "返回"
                //判断控制器的级数，第一级使用首页做为按钮的标题
                if childViewControllers.count == 1 {
                    title = childViewControllers.first?.title ?? "返回"
                }
                
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popToParent),isBack:true)
            }

        }
        
        super.pushViewController(viewController, animated: true)
    }

    
    @objc func popToParent(){
        popViewController(animated: true)
    }
}
